<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Mysql增删改查 &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0b ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       杨博的博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2021. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>Mysql增删改查</h1>
  <time datetime=2020-06-23T17:08:31&#43;0800 class="post-date">Tue, Jun 23, 2020</time>
  <p><strong>增</strong>：</p>
<p>1、save:</p>
<p>用法：给数据库表名赋值：例：user = model.User()</p>
<p>复制的变量就等于原表，给变量的想要添加的字段中添加值，</p>
<p>​     例：          user.name = &lsquo;杨博&rsquo;</p>
<p>save添加进表中：   user.save()</p>
<p>有外键的表添加数据：先查询出要添加的地方，因为要添加数据不是直接对应表，而是主键所在表中字段的数据，所以要往有外键的表中添加数据就要先查询出主键所对应的数据相对应的表                 s = models.School.objects.filter(&lsquo;积云教育&rsquo;).first()</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#960050;background-color:#1e0010">实例化表：</span> <span style="color:#66d9ef">class</span> <span style="color:#960050;background-color:#1e0010">= </span><span style="color:#a6e22e">models</span><span style="color:#f92672">.</span>Class()

class<span style="color:#f92672">.</span>name <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;1811&#39;</span>

class<span style="color:#f92672">.</span>school <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;s&#39;</span>      <span style="color:#75715e"># 外键所对应的主表</span>

class<span style="color:#f92672">.</span>save() 

<span style="color:#960050;background-color:#1e0010">要添加多条数据一条一条添加，注意所用的变量名不能相同</span>

</code></pre></div><p>​</p>
<p>2、直接添加：</p>
<p>例：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">model<span style="color:#f92672">.</span>User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>create(name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;杨博&#39;</span>)

  <span style="color:#960050;background-color:#1e0010">表名</span>           <span style="color:#960050;background-color:#1e0010">字段名</span>
</code></pre></div><p><strong>删：</strong></p>
<p>delete</p>
<p>先查出来再delete直接删除</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#ae81ff">1</span><span style="color:#960050;background-color:#1e0010">、</span>models<span style="color:#f92672">.</span>User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;杨博&#39;</span>)<span style="color:#f92672">.</span>first()<span style="color:#f92672">.</span>delete()
</code></pre></div><div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#ae81ff">2</span><span style="color:#960050;background-color:#1e0010">、</span>models<span style="color:#f92672">.</span>User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>get(name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;杨博&#39;</span>)<span style="color:#f92672">.</span>delete()
</code></pre></div><p><strong>改：</strong></p>
<p>1、先查出来要修改的内容，再用增加的方法增添</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">user <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>User<span style="color:#f92672">.</span>object<span style="color:#f92672">.</span>filter(name <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;杨博&#39;</span>)<span style="color:#f92672">.</span>first()

user<span style="color:#f92672">.</span>name <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;张三&#39;</span>

user<span style="color:#f92672">.</span>save()

</code></pre></div><p>2、update   查出来的必须是结果集（列表），也就是只能用filter方法查询，并且不能跟first().</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">Models<span style="color:#f92672">.</span>User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;杨博&#39;</span>)<span style="color:#f92672">.</span>update(name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;张三&#39;</span>)
</code></pre></div><p>html中修改数据，后台·post需要获取id用隐藏获取</p>
<!-- raw HTML omitted -->
<p>hidden  该行内容隐藏，用来传输id到post</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">defpost(self,request):

s_id<span style="color:#f92672">=</span>request<span style="color:#f92672">.</span>POST<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;id&#39;</span>)

name<span style="color:#f92672">=</span>request<span style="color:#f92672">.</span>POST<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;name1&#39;</span>)

grade<span style="color:#f92672">=</span>request<span style="color:#f92672">.</span>POST<span style="color:#f92672">.</span>get(<span style="color:#e6db74">&#39;grade&#39;</span>)

ifall([s_id,name,grade]):

s<span style="color:#f92672">=</span>models<span style="color:#f92672">.</span>Student<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>filter(id<span style="color:#f92672">=</span>s_id)<span style="color:#f92672">.</span>first()

s<span style="color:#f92672">.</span>name<span style="color:#f92672">=</span>name

s<span style="color:#f92672">.</span>grade<span style="color:#f92672">=</span>grade

s<span style="color:#f92672">.</span>save()

</code></pre></div><p><strong>查：</strong></p>
<p>1、get查询</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">name1 <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>User<span style="color:#f92672">.</span>objects<span style="color:#f92672">.</span>get(name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;杨博&#39;</span>)
</code></pre></div><p>2、filter</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">Name1 <span style="color:#f92672">=</span> models<span style="color:#f92672">.</span>User<span style="color:#f92672">.</span>object<span style="color:#f92672">.</span>filter(name<span style="color:#f92672">=</span><span style="color:#e6db74">&#39;杨博&#39;</span>)<span style="color:#f92672">.</span>first()
</code></pre></div><p>最后加上first结果是一个，例：杨博</p>
<p>不加是一个列表        例：[<a href="User:%E6%9D%A8%E5%8D%9A">User:杨博</a>]</p>
<p><strong>如果不行用变量接受</strong></p>

</div>


    </main>

    
  </body>
</html>
